<script lang="ts" setup>
import { deletePatientByIdAPI, getPatientMylistAPI } from '@/apis/patient'
import { useConsultStore } from '@/stores/counter'
import type { GetPatientItem } from '@/types/patient'
import { onReady, onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])

// 删除患者
const onSwipeActionClick = async (id: string, index: number) => {
  // 前端删除
  patientList.value.splice(index, 1)
  // 后端删除
  await deletePatientByIdAPI(id)
  //如果删除的是当前选中的患者，需要清空store中的patientId
  if(store.patientId === id){
    store.patientId=''
  }
}

// 获取患者列表,最多可添加6人
const patientList = ref<GetPatientItem[]>([])

// 获取患者列表
const getPatientList = async () => {
  const res = await getPatientMylistAPI()
  patientList.value = res.data
}

//页面onshow 显示时 获取患者列表
onShow(() => {
  getPatientList()
})

  // 获取页面参数
  const props = defineProps<{
    type?: 'consult'  // type 是可选参数
  }>()

  onReady(() => {
    // 动态设置标题写到 onReady 中兼容性更好
    uni.setNavigationBarTitle({ title: props.type === 'consult' ? '选择患者' : '患者列表' })
  })


  //选择患者
  const store = useConsultStore()
  const handleSelectPatient = (id:string)=>{
    if(props.type!=='consult')return
    store.patientId = id
  }

  //下一步跳转至待付款页面
  const goToPayment = ()=>{
    uni.navigateTo({url:'/subpkg_consult/pages/payment/payment'})
  }

</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="(patient, index) in patientList"
          :key="patient.id"
          :right-options="swipeOptions"
          @click="onSwipeActionClick(patient.id, index)"
        >
          <view 
          @click="handleSelectPatient(patient.id)"
          :class="{
              active:
                props.type === 'consult' ? patient.id === store.patientId : patient.defaultFlag === 1,
            }" class="archive-card">
            <view class="archive-info" @click="handleSelectPatient(patient.id)">
              <text class="name">{{ patient.name }}</text>
              <text class="id-card">{{
                patient.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text v-if="patient.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info"  @click="handleSelectPatient(patient.id)">
              <text class="gender">{{ patient.genderValue }}</text>
              <text class="age">{{ patient.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/pages/form/form?id=${patient.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/pages/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>

      <!-- 下一步按钮 -->
        <view class="next-step" v-if="$props.type === 'consult'">
          <button  
          :disabled="store.patientId===''"
          class="uni-button"
          @click="goToPayment"
          >下一步</button>
        </view>

    </view>
  </scroll-page>
</template>

<style lang="scss">
@import './index.scss';

.next-step {
    position: fixed;
    right: 0;
    left: 0;
    // 底部安全区距离
    bottom: env(safe-area-inset-bottom);
    z-index: 100;
    padding: 30rpx 40rpx;
    background-color: #fff;
  }
</style>
